package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
   public PageDTO page(Integer page, Integer size ){
        Page<Item> pageinfo = new Page<>(page, size);
        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<>();
      //  lqw.orderByAsc(Item::getUpdateTime);
       itemService.page(pageinfo, lqw);
        // 当前页条数
        List<Item> records = pageinfo.getRecords();
        //总条数
        long total = pageinfo.getTotal();
        return new PageDTO(total,records);
    }

    /**
     * 根据商品id 查询
     * @param id
     */
    @GetMapping("/{id}")
    public Item getByid(@PathVariable  Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增商品
     * @param item
     */
   @PostMapping
   public void save(@RequestBody Item item){
//        item.setCreateTime(new Date());
//        item.setUpdateTime(new Date());
        itemService.save(item);
      rabbitTemplate.convertAndSend("item.topic","item.insert",item.getId());

   }

    /**
     * id：商品id，status：商品状态 1-代表上架 2-代表下架
     * 商品的上下架
     * @param id
     */
   @PutMapping("/status/{id}/{status}")
   public void under(@PathVariable Integer status,@PathVariable Long id){
       LambdaUpdateWrapper<Item> lqw = new LambdaUpdateWrapper<>();
       lqw.in(Item::getId, id);
       lqw.set(Item::getStatus, status);
       itemService.update(lqw);
       if (status ==1){
           rabbitTemplate.convertAndSend("item.topic","item.insert",id);
       }else {
           rabbitTemplate.convertAndSend("item.topic","item.delete",id);
       }

   }

    /**
     * 修给商品
     * @param item
     */
    @PutMapping
    public void updata(@RequestBody Item  item){

       itemService.updateById(item);
   }

    /**
     * 根据id 删除
     * @param id
     */
    @DeleteMapping("/{id}")
   public void deleteByid(@PathVariable Long id){
        itemService.removeById(id);
        rabbitTemplate.convertAndSend("item.topic","item.delete",id);
   }
    /*
     * 查询商品总数
     * @return
     */
    @GetMapping("/total")
    public Integer getTotal() {
        int count = itemService.count();
        return count;
    }


    /**
     *  扣减库存接口
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable Long itemId, @PathVariable Integer num){
        Item item = itemService.getById(itemId);
        Integer stock = item.getStock();
        if (num!= null && num<= stock){
            int kuchu = stock - num;
            item.setStock(kuchu);
        }
        itemService.updateById(item);
    }

}
